package org.coderfun.member.core.service;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.coderfun.member.core.dao.MemberDAO;
import org.coderfun.member.core.entity.Member;
import org.coderfun.member.core.entity.Member_;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import klg.common.dataaccess.BaseServiceImpl;
import klg.query.jpa.expr.AExpr;



/**
 *
 * 
 * Generated by fieldmeta at 2018-12-11T21:24:49+08:00
 *
 */
 
@Service
public class MemberServiceImpl  extends BaseServiceImpl<Member, Long> implements MemberService{
	@Autowired
	MemberDAO memberDAO;
	
	private void encryptPassword(Member member) {
		String salt = RandomStringUtils.randomAlphanumeric(32);
		member.setSalt(salt);
		member.setPassword(encryptPassword(member.getPlainPassword(), salt));
	}
	
	private String encryptPassword(String plainPassword,String salt){
		//md5 加密
		return DigestUtils.md5Hex(salt+plainPassword);
	}
	
	@Override
	public Member getByEmail(String email) {
		return memberDAO.getOne(AExpr.eq(Member_.email, email));	
	}

	@Override
	public Member getByUsername(String username) {
		// TODO Auto-generated method stub
		return memberDAO.getOne(AExpr.eq(Member_.username, username));
	}

	@Override
	public Member getByMobile(String mobile) {
		// TODO Auto-generated method stub
		return memberDAO.getOne(AExpr.eq(Member_.mobile, mobile));
	}
	
	
	@Override
	public boolean checkPassword(Member member) {
		// TODO Auto-generated method stub
		String password = encryptPassword(member.getPlainPassword(), member.getSalt());		
		return password.equals(member.getPassword());
	}

	@Override
	public boolean checkUsernameExist(String username) {
		// TODO Auto-generated method stub
		Member member = getByUsername(username);
		return member != null;
	}

	@Override
	public boolean checkEmailExist(String email) {
		// TODO Auto-generated method stub
		Member member = getByEmail(email);
		return member != null;
	}

	@Override
	public boolean checkMobileExist(String mobile) {
		// TODO Auto-generated method stub
		Member member = getByMobile(mobile);
		return member != null;
	}

	@Transactional
	@Override
	public Member create(Member member) {
		// TODO Auto-generated method stub
		encryptPassword(member);
		return memberDAO.save(member);
	}

	@Transactional
	@Override
	public void updatePassword(Long memberId, String plainPassword) {
		// TODO Auto-generated method stub
		Member member = memberDAO.getById(memberId);
		member.setPlainPassword(plainPassword);
		encryptPassword(member);
		memberDAO.update(member);
	}
	
	
	@Transactional
	@Override
	public void changePassword(Long memberId, String oldPlainPassword, String newPlainPassword) {
		// TODO Auto-generated method stub
		Member member = memberDAO.getById(memberId);
		member.setPlainPassword(oldPlainPassword);
		if(this.checkPassword(member)){
			member.setPlainPassword(newPlainPassword);
			encryptPassword(member);
		}
		memberDAO.update(member);
	}
}
